/**
 * 文章模块
 * author: zhuoyifan
 * 2020-11-29
 */
import binarySearch from "@/js/binarySearch";

const state = {
  // TODO 日期需要自动生成——发表时的日期
  articles: [
    {id: 1, title: 'constructor属性', description: '...', content: '.....', date: '2020-11-29'},
    {id: 2, title: 'java', description: '...', content: '.....', date: '2020-1-1'},
    {id: 3, title: 'javascript', description: '...', content: '.....', date: '2019-2-3'},
  ],
};

const mutations = {
  addArticle(state, newArticle) {
    state.articles.push(newArticle);
  },
  deleteArticle(state, articleId) {
    let index = binarySearch(state.articles, articleId);
    state.articles.splice(index, 1);
  }
};

const actions = {
  addArticleAction({commit}, newArticle) {
    commit('addArticle', newArticle);
  },
  deleteArticleAction({commit}, articleId) {
    commit('deleteArticle', articleId);
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}




